<script setup >
import { storeToRefs } from 'pinia'
import { useStore } from '../store/index'
const user = useStore()
//storeToRefs
const { count, status } = storeToRefs(user)

const handClick = () => {
  user.$patch(state => {
    {
      ;(state.status = user.count > 10 ? false : true),
        (state.count = user.status ? user.count + 1 : user.count - 1)
    }
  })
}

watch(
  user,
  state => {
    // 每当它发生变化时，将整个状态持久化到本地存储
    console.log(state,'监听')
     localStorage.setItem('piniaState', JSON.stringify(state))
  },
  { deep: true }
)
</script>

<template>
  <h3>{{ count }}</h3>
  <h3>{{ status }}</h3>
  <button @click="handClick">增加</button>
</template>
